package com.yellowpage.omc.ability.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.nsp.data.NspFetchLog;
import com.yellowpage.omc.ability.data.Partner;
import com.yellowpage.omc.ability.service.PartnerService;

public class PartnerAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<Partner> partnerList = new ArrayList<Partner>();
	private List<Map> resultList = new ArrayList<Map>();

	private Partner partner;
	private PartnerService partnerService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_partnerId;
	private String query_partnerCode;
	private String query_partnerKey;
	private String query_contactName;
	private String query_contactTel;
	private String query_contactAddress;
	private String query_contactNote;
	private String query_techContactName;
	private String query_techContactTel;
	private String query_techContactNote;
	private String query_note;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_address;
	private String query_name;
	private String query_status;
	
	private String query_LastSynchronizationUser;
	private String query_LastSynchronizationTime;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.partner = partnerService
					.getPartner(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("partnerId", query_partnerId);
		queryMap.put("partnerCode", query_partnerCode);
		queryMap.put("partnerKey", query_partnerKey);
		queryMap.put("contactName", query_contactName);
		queryMap.put("contactTel", query_contactTel);
		queryMap.put("contactAddress", query_contactAddress);
		queryMap.put("contactNote", query_contactNote);
		queryMap.put("techContactName", query_techContactName);
		queryMap.put("techContactTel", query_techContactTel);
		queryMap.put("techContactNote", query_techContactNote);
		queryMap.put("note", query_note);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("lastUpdateUser", query_lastUpdateUser);
		queryMap.put("lastUpdateTime", query_lastUpdateTime);
		queryMap.put("address", query_address);
		queryMap.put("name", query_name);
		queryMap.put("status", query_status);
		queryMap.put("LastSynchronizationUser", query_LastSynchronizationUser);
		queryMap.put("LastSynchronizationTime", query_LastSynchronizationTime);

		resultList = partnerService.queryPartnerByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	
	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		partner.setCreateTime(new Date());
		partner.setCreateUser(loginUser.getUserId());
		partner.setLastUpdateTime(new Date());
		partner.setLastUpdateUser(loginUser.getUserId());
		partner.setStatus("1");

		partnerService.savePartner(partner);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		
		
		if (partner.getAddress().length() == 0) {
			this.addActionError("** 地址 不能为空 !");
		}
		if (partner.getName().length() == 0) {
			this.addActionError("** 合作商名称 不能为空 !");
		}
		
		if (partner.getContactName().length() == 0) {
			this.addActionError("** 联系人 不能为空 !");
		}
		if (partner.getContactTel().length() == 0) {
			this.addActionError("** 联系电话不能为空 !");
		}
		
			
	
		this.setValidatorResult("/omc/ability/PartnerAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		partner = partnerService.getPartner(id);

		return RESULT_EDIT;
	}

	public String update() {
		
		partner.setLastUpdateTime(new Date());
		partner.setLastUpdateUser(loginUser.getUserId());

		partnerService.updatePartner(partner);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {

		if (partner.getAddress().length() == 0) {
			this.addActionError("** 地址 不能为空 !");
		}
		if (partner.getName().length() == 0) {
			this.addActionError("** 合作商名称 不能为空 !");
		}
		
		if (partner.getContactName().length() == 0) {
			this.addActionError("** 联系人 不能为空 !");
		}
		if (partner.getContactTel().length() == 0) {
			this.addActionError("** 联系电话不能为空 !");
		}
		
		
		
		this.setValidatorResult("/omc/ability/PartnerUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.partnerService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突，删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setPartner(Partner partner) {
		this.partner = partner;
	}

	public Partner getPartner() {
		return partner;
	}

	public void setPartnerService(PartnerService partnerService) {
		this.partnerService = partnerService;
	}

	public List<Partner> getPartnerList() {
		return partnerList;
	}

	public void setPartnerList(List<Partner> partnerList) {
		this.partnerList = partnerList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_partnerId() {
		return this.query_partnerId;
	}

	public void setQuery_partnerId(String partnerId) {
		this.query_partnerId = partnerId;
	}

	public String getQuery_partnerCode() {
		return this.query_partnerCode;
	}

	public void setQuery_partnerCode(String partnerCode) {
		this.query_partnerCode = partnerCode;
	}

	public String getQuery_partnerKey() {
		return this.query_partnerKey;
	}

	public void setQuery_partnerKey(String partnerKey) {
		this.query_partnerKey = partnerKey;
	}

	public String getQuery_contactName() {
		return this.query_contactName;
	}

	public void setQuery_contactName(String contactName) {
		this.query_contactName = contactName;
	}

	public String getQuery_contactTel() {
		return this.query_contactTel;
	}

	public void setQuery_contactTel(String contactTel) {
		this.query_contactTel = contactTel;
	}

	public String getQuery_contactAddress() {
		return this.query_contactAddress;
	}

	public void setQuery_contactAddress(String contactAddress) {
		this.query_contactAddress = contactAddress;
	}

	public String getQuery_contactNote() {
		return this.query_contactNote;
	}

	public void setQuery_contactNote(String contactNote) {
		this.query_contactNote = contactNote;
	}

	public String getQuery_techContactName() {
		return this.query_techContactName;
	}

	public void setQuery_techContactName(String techContactName) {
		this.query_techContactName = techContactName;
	}

	public String getQuery_techContactTel() {
		return this.query_techContactTel;
	}

	public void setQuery_techContactTel(String techContactTel) {
		this.query_techContactTel = techContactTel;
	}

	public String getQuery_techContactNote() {
		return this.query_techContactNote;
	}

	public void setQuery_techContactNote(String techContactNote) {
		this.query_techContactNote = techContactNote;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_address() {
		return this.query_address;
	}

	public void setQuery_address(String address) {
		this.query_address = address;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	/**
	 * @return the query_LastSynchronizationUser
	 */
	public String getQuery_LastSynchronizationUser() {
		return query_LastSynchronizationUser;
	}

	/**
	 * @param query_LastSynchronizationUser the query_LastSynchronizationUser to set
	 */
	public void setQuery_LastSynchronizationUser(
			String query_LastSynchronizationUser) {
		this.query_LastSynchronizationUser = query_LastSynchronizationUser;
	}

	/**
	 * @return the query_LastSynchronizationTime
	 */
	public String getQuery_LastSynchronizationTime() {
		return query_LastSynchronizationTime;
	}

	/**
	 * @param query_LastSynchronizationTime the query_LastSynchronizationTime to set
	 */
	public void setQuery_LastSynchronizationTime(
			String query_LastSynchronizationTime) {
		this.query_LastSynchronizationTime = query_LastSynchronizationTime;
	}

}
